package com.odm.communitymanage.ui.activity

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.core.bean.MyActivityData
import com.odm.communitymanage.util.GlideUtils
import com.odm.communitymanage.widget.AvatarModeUtils

/**
 * @description: 活动列表适配器
 * @author: ODM
 * @date: 2020/4/20
 */
class ActivitiesAdapter(data : MutableList<MyActivityData>)  :
   BaseQuickAdapter<MyActivityData , BaseViewHolder>(R.layout.item_activity , data){


    override fun convert(holder: BaseViewHolder, item: MyActivityData) {
        holder.setText(R.id.tvContactInfo_Activities , item.name )
        holder.setText(R.id.tvTime_Activities , item.time )
        holder.setText(R.id.tvTopic_Activities , item.topic )
        holder.setText(R.id.tvDescription_Activities , item.description )

        if(item.status == 0){
            //进行中
            holder.setText(R.id.tvState_Activities , context.getString(R.string.ongoing))
            holder.getViewOrNull<TextView>(R.id.tvState_Activities)?.setTextColor(ColorUtils.getColor(R.color.activity_doing))

        }else if(item.status == 1){
            //已结束
            holder.setText(R.id.tvState_Activities , context.getString(R.string.over))
            holder.getViewOrNull<TextView>(R.id.tvState_Activities)?.setTextColor(ColorUtils.getColor(R.color.activity_done))
        }

        //加载默认的用户头像
        val avatarView  : ImageView ?= holder.getView(R.id.ivAvatar_Activities)
        avatarView?.let {
            val name = item.name
            if(name != ""){
                //加载仿钉钉头像
                val para: ViewGroup.LayoutParams = it.layoutParams
                val w = para.width
                val h = para.height
                GlideUtils.getInstance().loadBitmap(
                    context,
                    AvatarModeUtils.getAvatar(arrayListOf(name[0].toString()) as List<Any>?, w , h),
                    it
                )
            }
        }

        //按顺序加载图片,当图片列表 不为空
        item.imgs?.let {
            //获取对应控件加载图片
            val iv0 : ImageView = holder.getView<ImageView>(R.id.ivPhoto_0_Activities)
            val iv1 : ImageView = holder.getView<ImageView>(R.id.ivPhoto_1_Activities)
            val iv2 : ImageView = holder.getView<ImageView>(R.id.ivPhoto_2_Activities)
            val imageViews: Array<ImageView> = arrayOf(iv0, iv1, iv2)
            if (item.imgs.isEmpty()) {
                //无图片时全部清空遗留的viewHolder
                for (iv in imageViews) {
//                    Glide.with(context).clear(iv)
                    iv.visibility = View.GONE
                }
                return
            }
            //按顺序加载图片
            if (item.imgs.isNotEmpty()) {
                //根据图片数量限制 item的图片显示
                when(item.imgs.size){
                    1 -> {
                        imageViews[1].visibility = View.GONE
                        imageViews[2].visibility = View.GONE
                    }
                    2 -> {
                        imageViews[2].visibility = View.GONE
                    }
                }
                for (index in item.imgs.indices) {
                    //三个ImageView中，前一个ImageView未被显示，当前位置的ImageView不能被显示
                    var errorFlag = false
                    when(index){
                        1,2 -> {
                            if(imageViews[index-1].visibility != View.VISIBLE){
                                errorFlag = true
                            }
                        }
                    }
                    if(errorFlag){
                        imageViews[index].visibility = View.GONE
                    }else {
                        val url = item.imgs[index]
                        GlideUtils.getInstance()
                            .loadImageUrl(context, url, imageViews[index])
                        imageViews[index].visibility = View.VISIBLE

//                        val tag = imageViews[index].getTag(R.id.recyclerview_image_url)
//                        if (tag != null && tag != url) {
//                            Glide.with(context).clear(imageViews[index])
//                            imageViews[index].visibility = View.GONE
//                        } else {
//                            GlideUtils.getInstance()
//                                .loadImageUrl(context, url, imageViews[index])
//                            imageViews[index].visibility = View.VISIBLE
//                            imageViews[index].setTag(R.id.recyclerview_image_url, url)
//                        }
                    }
                }
            }

        }

    }
}

    class ActivitiesDiffCallback(private val oldList: List<MyActivityData> ?, private val newList: List<MyActivityData> ?) :
        DiffUtil.Callback() {

        override fun getOldListSize(): Int {
            return oldList?.size ?: 0
        }

        override fun getNewListSize(): Int {
            return newList?.size ?: 0
        }

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList?.get(oldItemPosition)?.javaClass == newList?.get(newItemPosition)?.javaClass
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return  oldList?.get(oldItemPosition)?.id == newList?.get(newItemPosition)?.id &&
                    oldList?.get(oldItemPosition)?.imgs == newList?.get(newItemPosition)?.imgs
        }
    }